<!-- subpkg_medicine/payment/payment.vue -->
<script lang="ts" setup>
  import { addressListApi, createOrderApi, preOrderApi } from '@/api/order'
  import type { Address, OrderPre } from '@/types/order'
  import { ref } from 'vue'

  // 接收路由参数
  interface Props {
    id: string
  }
  const props = defineProps<Props>()

  // 1. 收货地址信息
  const addressInfo = ref({} as Address)
  // 用户收货地址
  const getAddressList = async () => {
    // 地址列表接口
    const data = await addressListApi()
    // 渲染地址列表数据
    addressInfo.value = data[0]
  }
  // 获取收货地址列表
  getAddressList()

  const preOrderInfo = ref({} as OrderPre)
  const getPreOrderInfo = async () => {
    const res = await preOrderApi(props.id)
    preOrderInfo.value = res
  }
  getPreOrderInfo()

  const isShow = ref(false)

  const onCreateOrder = async () => {
    const res = await createOrderApi(props.id, addressInfo.value.id)
    console.log('res  ----->  ', res)
    isShow.value = true
  }
</script>

<template>
  <scroll-page background-color="#f6f6f6">
    <view class="medicine-page">
      <view class="page-header">
        <view class="order-shippment">
          <view class="region">
            <uni-icons size="16" color="#FF7702" type="location-filled" />
            {{ addressInfo.city }} {{ addressInfo.county }}
          </view>
          <view class="detail">{{ addressInfo.addressDetail }}</view>
          <view class="receiver"> {{ addressInfo.receiver }} {{ addressInfo.mobile }} </view>
        </view>
      </view>

      <view class="order-shop">
        优医药房
        <text class="alt">优医质保 假一赔十</text>
      </view>
      <!-- 药品列表 -->
      <view class="medicine-list" v-for="item in preOrderInfo.medicines" :key="item.id">
        <view class="medicine-list-item">
          <image class="medicine-cover" :src="item.avatar" mode="aspectFill" />
          <view class="medicine-info">
            <text class="name">{{ item.name }}</text>
            <text class="unit symbol">{{ item.specs }}</text>
            <text class="price">¥{{ item.amount }}</text>
          </view>
          <view class="quantity">x{{ item.quantity }}</view>
          <view class="guide">用法用量：{{ item.usageDosag }}</view>
        </view>
      </view>

      <!-- 订单信息 -->
      <!-- 订单信息 -->
      <view class="order-info">
        <uni-list :border="false">
          <uni-list-item title="药品金额" :right-text="'¥' + preOrderInfo.payment" />
          <uni-list-item title="运费" :right-text="'¥' + preOrderInfo.expressFee" />
          <uni-list-item
            title="优惠券"
            show-arrow
            :right-text="'-¥' + preOrderInfo.couponDeduction"
          />
          <uni-list-item title="实付款" :right-text="'¥' + preOrderInfo.actualPayment" />
        </uni-list>
      </view>

      <!-- 底部 -->
      <view class="toolbar">
        <view class="total-amount">
          需付款: <text class="number">¥{{ preOrderInfo.actualPayment }}</text>
        </view>
        <view class="buttons">
          <button class="uni-button" @click="onCreateOrder">立即支付</button>
        </view>
      </view>
    </view>
  </scroll-page>

  <!-- :show -->
  <!-- @update:show -->
  <custom-payment :amount="preOrderInfo.actualPayment" :show="isShow" @on-cancel="isShow = false" />
</template>

<style lang="scss">
  @import './index.scss';
</style>
